using System;
using System.Data;
using System.Collections;
using istIUniDB;

namespace Shraga.BLL
{
	[DBFunctions("", "")]
	public class Candidate : DBObject
	{
		#region Data Members

		private Hashtable m_CandidacyDetails = null;
		private Hashtable m_HashtafotDetails;
		private Hashtable m_PotentialDetails;
		
		private String	  m_sFirstName;
		private String	  m_sLastName;

		#endregion

		#region Properties

		public int		 CandidateId { get { return base.PrimaryKey; } }		
		public String	 FirstName 
		{ 
			get 
			{
				if (m_sFirstName == "") PopulateCandidateName();
				return m_sFirstName;
			}
		}

		public String	 LastName 
		{
			get 			 
			{
				if (m_sLastName == "") PopulateCandidateName();
				return m_sLastName;
			} 
		}		

		public Hashtable Candidacies 
		{ 
			get 
			{ 
				if (m_CandidacyDetails == null)
				{					
					IUniDB UniDB = UniDBSingleton.Instance().UniDB;			
		
					clsParameter[] arrParam = new clsParameter[1];
					arrParam[0] = new clsParameter(CandidateId, OracleTypes.Int32);
					
					#region Load the Candidacies

					DataTable dtCandidacies = UniDB.RunStoredFunctionReturnDataTable("shraga_candidate.get_candidacies", ref arrParam);

					#region Populates Data
					
					m_CandidacyDetails = new Hashtable();
					foreach (DataRow drCurrent in dtCandidacies.Rows)
					{
						Candidacy curCandidacy = 
							(Candidacy)DBObjectsObserver.Instance().GetDBObject(typeof(Candidacy), 
												Convert.ToInt32(drCurrent["PRIMARY_KEY"]),
												new object[] { drCurrent });

						curCandidacy.DBObjectUpdated +=new dbobjectUpdated(SonDBObjectUpdated);
						m_CandidacyDetails.Add(curCandidacy.CandidacyId,curCandidacy);
					}

					#endregion
				
					#endregion					
				}

				return m_CandidacyDetails; 
			}
		}
		public Hashtable HashtafotHistory { get { return null; } }				
		public Hashtable Potentials { get { return null; } }				

		#endregion

		#region Constructors
			
		public Candidate(int IdNumber) : base()
		{
			DataTable dt = new DataTable();
			dt.Columns.AddRange(new DataColumn[] {new DataColumn("PRIMARY_KEY", typeof(int)), new DataColumn("LAST_UPDATE", typeof(DateTime))});

			DataRow drNew = dt.NewRow();
			drNew["PRIMARY_KEY"] = IdNumber;
			drNew["LAST_UPDATE"] = DateTime.Now;			

			dt.Rows.Add(drNew);

			m_drRawData = drNew;
		}

		#endregion

		#region Public Candidate Methods
	
		public void Hastefh(Destination NewDest, int ReasonId, int HashtafotTypeId, Destination Destination) {}
		public void ChangeCandidacy (Candidacy CandidacyToChange,int NewCandidacyStatus) {}
		public bool CreateLetter(int FormatId, DateTime SendDate, int SenderId, String Comment, String Description, int PackId)
		{
			try
			{
				clsParameter[] arrParam;
				arrParam = new clsParameter[7];
				arrParam[0] = new clsParameter(CandidateId, OracleTypes.Int32);
				arrParam[1] = new clsParameter(FormatId, OracleTypes.Int32);
				arrParam[2] = new clsParameter(SendDate, OracleTypes.Date);
				arrParam[3] = new clsParameter(SenderId, OracleTypes.Int32);
				arrParam[4] = new clsParameter(Comment, OracleTypes.Varchar2);
				arrParam[5] = new clsParameter(PackId, OracleTypes.Int32);
				arrParam[5] = new clsParameter(PackId, OracleTypes.Int32);
				arrParam[6] = new clsParameter(Description, OracleTypes.Varchar2);				

				return (Convert.ToBoolean(UniDBSingleton.Instance().UniDB.RunStoredFunctionReturnInteger("shraga_letter.Create_Note_Letter", ref arrParam)));
			}
			catch (Exception ex)
			{
				return false;
			}
		}

		public DataTable GetEventsTable()
		{
			clsParameter[] arrParam;
			arrParam = new clsParameter[1];
			arrParam[0] = new clsParameter(CandidateId, OracleTypes.Int32);

			return (UniDBSingleton.Instance().UniDB.RunStoredFunctionReturnDataTable("shraga_candidate_node.get_candidate_events", ref arrParam));
		}

		#endregion

		#region Private Methodes
		
		private void PopulateCandidateName()
		{
			CandidatesList cndl = new CandidatesList(CandidateId.ToString());
			m_sFirstName = EmptyData.StringValue(cndl.Names.Rows[0]["first"]);
			m_sLastName  = EmptyData.StringValue(cndl.Names.Rows[0]["last"]);
		}
			
		public override void Refresh(Type MarshaledType, int MarshaledId)
		{
			if (MarshaledType == typeof(Candidacy))
			{
				if (m_CandidacyDetails != null)
				{
					Candidacy currCnd =(Candidacy)DBObjectsObserver.Instance().GetDBObject(typeof(Candidacy), 
						MarshaledId, 
						new object[] {MarshaledId});
					currCnd.DBObjectUpdated += new dbobjectUpdated(SonDBObjectUpdated); 
					m_CandidacyDetails.Add(currCnd.CandidacyId, currCnd);				
					base.PopEvent(currCnd);
				}
			}
		}
		private void SonDBObjectUpdated(DBObject sender, DBObject updatedSon)
		{
			if (updatedSon == null)
			{
				base.PopEvent(sender);
			}
			else
			{
				base.PopEvent(updatedSon);
			}
		}
		#endregion

	}
}